"""
多元线性回归
"""
import numpy as np
import pandas as pd
import matplotlib.pyplot as plt
import plotly
import plotly.graph_objs as go
from linear_regression import LinearRegression

plotly.offline.init_notebook_mode()

# 得到训练和测试数据
data = pd.read_csv('../data/world-happiness-report-2017.csv')
train_data = data.sample(frac=0.8)      # sample随机抽取数据，frac抽取的比例
test_data = data.drop(train_data.index)     # 去除train_data数据，剩下的就当作测试集

input_param_name_1 = 'Economy..GDP.per.Capita.'
input_param_name_2 = 'Freedom'
output_param_name = 'Happiness.Score'

x_train = train_data[[input_param_name_1, input_param_name_2]].values
y_train = train_data[[output_param_name]].values

x_test = test_data[[input_param_name_1, input_param_name_2]].values
y_test = test_data[[output_param_name]].values

# 数据分布散点图
plot_train_trace = go.Scatter3d(
    x=x_train[:, 0].flatten(),
    y=x_train[:, 1].flatten(),
    z=y_train.flatten(),
    name='Train data',
    mode='markers',
    marker=dict(
        size=12,
        opacity=0.8     # 透明度
    )
)

plot_test_trace = go.Scatter3d(
    x=x_test[:, 0].flatten(),
    y=x_test[:, 1].flatten(),
    z=y_test.flatten(),
    name='Test data',
    mode='markers',
    marker=dict(
        size=12,
        opacity=0.8
    )
)

plot_layout = go.Layout(
    title='Data Sets',
    scene={
        'xaxis': dict(title=input_param_name_1),
        'yaxis': dict(title=input_param_name_2),
        'zaxis': dict(title=output_param_name)
    },
    margin=dict(l=0, r=0, b=0, t=0)
)

num_iterations = 500
learning_rate = 0.01

linear_regression = LinearRegression(x_train, y_train)
theta, cost_history = linear_regression.train(learning_rate, num_iterations)

print('开始时的损失:', cost_history[0])
print('训练后的损失:', cost_history[-1])

# 训练梯度下降曲线图
plt.plot(range(num_iterations), cost_history)
plt.xlabel('Iter')
plt.ylabel('Cost')
plt.title('Gradient Descent Progress')
plt.show()

predictions_num = 10

x_min = x_train[:, 0].min()
x_max = x_train[:, 0].max()
y_min = x_train[:, 1].min()
y_max = x_train[:, 1].max()

x_axis = np.linspace(x_min, x_max, predictions_num)
y_axis = np.linspace(y_min, y_max, predictions_num)

x_predictions = np.zeros((predictions_num * predictions_num, 1))
y_predictions = np.zeros((predictions_num * predictions_num, 1))

x_y_index = 0
for x_index, x_value in enumerate(x_axis):
    for y_index, y_value in enumerate(y_axis):
        x_predictions[x_y_index] = x_value
        y_predictions[x_y_index] = y_value
        x_y_index += 1

z_predictions = linear_regression.predict(np.hstack((x_predictions, y_predictions)))

plot_predictions_trace = go.Scatter3d(
    x=x_predictions.flatten(),
    y=y_predictions.flatten(),
    z=z_predictions.flatten(),
    name='Prediction',
    mode='markers',
    marker=dict(
        size=1
    ),
    opacity=0.8,
    surfaceaxis=2
)

plot_data = [plot_train_trace, plot_test_trace, plot_predictions_trace]
plot_figure = go.Figure(data=plot_data, layout=plot_layout)
plotly.offline.plot(plot_figure)
